package com.cmcc.passenger.ui.places;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.cmcc.passenger.R;
import com.cmcc.passenger.data.entity.PlaceAutoComplete;

import java.util.List;


public class AdrressSearchAdapter extends RecyclerView.Adapter<AdrressSearchAdapter.ViewHolder> {
    private Context mContext;
    private List<PlaceAutoComplete> datas;
    private AdrressSearchListener listener;
    interface  AdrressSearchListener{
        void clickSearch(PlaceAutoComplete data);
        void clickAdd(PlaceAutoComplete data);
    }
    public AdrressSearchAdapter(Context context, List<PlaceAutoComplete> datas, AdrressSearchListener listener) {
        mContext = context;
        this.datas = datas;
        this.listener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.list_item_search_result, parent, false);
        ViewHolder viewHolder = new ViewHolder(view, listener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
            holder.ivFavorite.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PlaceAutoComplete d = datas.get(position);
                    listener.clickAdd(d);
                }
            });
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PlaceAutoComplete d = datas.get(position);
                    listener.clickSearch(d);
                }
            });
            holder.textView2.setText(datas.get(position).getFirstAddress());
            holder.textView3.setText(datas.get(position).getSecondAddress());
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView textView3;
        private TextView textView2;
        private ImageView ivFavorite;
        private View itemView;




        public ViewHolder(final View itemView, final AdrressSearchListener listener) {
            super(itemView);
            this.itemView = itemView;
            textView3 = (TextView) itemView.findViewById(R.id.textView3);
            textView2 = (TextView) itemView.findViewById(R.id.textView2);
            ivFavorite = (ImageView) itemView.findViewById(R.id.iv_favorite);

        }

    }
}
